// index.js
// 获取应用实例
import consts from '../../js/const.js';
import Toast from '@vant/weapp/toast/toast';
import {routerFilter} from '../../utils/routerFilter';
import util from "../../utils/util";
import calcDate from "../../js/calcDate";

const app = getApp();

routerFilter({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
        , clockUrl: consts.getImageUrl('clock.png'),
        previousMargin: 0,
        nextMargin: 0,
        operateItems: [{
            backgroundUrl: consts.getImageUrl('item1.png'),
            subBackgroundUrl: consts.getImageUrl('item1-1.png'),
            remindImageUrl: consts.getImageUrl('item1-2.png'),
            labelZh: '车辆年检',
            labelEn: 'yearly inspection',
            invoke: 'checkCar'
        },
            {
                backgroundUrl: consts.getImageUrl('item2.png'),
                subBackgroundUrl: consts.getImageUrl('item2-1.png'),
                labelZh: '二手置换',
                labelEn: 'used car',
                invoke: 'usedCarValuation'
            }, {
                backgroundUrl: consts.getImageUrl('item3.png'),
                subBackgroundUrl: consts.getImageUrl('item3-1.png'),
                labelZh: '维修专家',
                labelEn: 'Repair personnel',
                invoke: 'vehicleRepair'
            }, {
                backgroundUrl: consts.getImageUrl('item4.png'),
                subBackgroundUrl: consts.getImageUrl('item4-1.png'),
                labelZh: '分享获益',
                labelEn: 'Sharing benefits',
                invoke: 'checkCar'
            }],
        moreAppImageUrl: consts.getImageUrl('more-app.png'),
        scanImageUrl: consts.getImageUrl('scan.png')
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad() {
        let showRemark = function () {
            util.promise('/baseConfig/searchConfigByTypeCdes', [consts.SP_HP_MEMO]).then(res => {
                if (res.data) {
                    that.setData({spHpMemo: util.isEmptyObject(res.data[consts.SP_HP_MEMO]) ? null : res.data[consts.SP_HP_MEMO][0].dcOptions})
                }
            })
        };
        let that = this;
        that.isLogin(null, function () {
            // Promise.all('',)
            util.promise('/searchEycVehicleFileByUserId').then(function (res) {
                let vehicleFiles = res.data;
                if (!util.isEmptyObject(vehicleFiles)) {
                    that.setData({vehicleFiles: vehicleFiles});
                    vehicleFiles.forEach(function (vehicleFile) {
                        util.promise('/baseConfig/searchMatchInspRule', vehicleFile).then(function (res) {
                            if (res.data) {
                                if(vehicleFile.registerDt){
                                    vehicleFile.discreetDays=calcDate.calcDateDayDifference(calcDate.caleInspEndDate(new Date(vehicleFile.registerDt),res.data),new Date());
                                }
                                that.setData({vehicleFiles: vehicleFiles});
                            }
                        });
                    });
                } else {
                    showRemark();
                }
            });
        }, function () {
            showRemark();
        });
        util.promise('/baseConfig/searchPublishInfoByTypeCdes', ['NEWS', 'NOTICE'/*, 'CAROUSEL'*/]).then(function (res) {
            if (res.data) {
                let data = util.groupBy(res.data, 'type');
                that.setData({
                    // carousels: data['CAROUSEL'],
                    newItems: data['NEWS'],
                    notice: util.isEmptyObject(data['NOTICE']) ? null : data['NOTICE'][0]
                });
            }
        });
        util.promise('/baseConfig/searchCarouselPictures').then(function (res) {
            that.setData({
                carousels: res.data
            });
        });
    },
    selectOpr(e) {
        this[e.currentTarget.dataset.invoke].call(this);
    },
    usedCarValuation(e) {
        wx.navigateTo({
            url: '/pages/vehicle/vehicleValuation',
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                acceptDataFromOpenedPage: function (data) {
                    console.log(data)
                },
                someEvent: function (data) {
                    console.log(data)
                }
            },
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', {data: 'test'})
            }
        })
    },
    checkCar(e) {
        this.isLogin(null, function () {
            wx.navigateTo({
                url: '/pages/vehicle/checkVehicle',
                events: {
                    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                    acceptDataFromOpenedPage: function (data) {
                    },
                    someEvent: function (data) {
                    }
                },
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', {data: 'test'})
                }
            })
        });

    },
    vehicleRepair(e){
        this.isLogin(null, function () {
            wx.navigateTo({
                url: '/pages/vehicle/vehicleRepair',
                events: {
                    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                    acceptDataFromOpenedPage: function (data) {
                    }
                },
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', {data: 'test'})
                }
            })
        });
    },
    switchCar(e) {
        wx.switchTab({
            url: '/pages/vehicle/vehicleFileList'
        })
    },
    newsList(e){
        wx.navigateTo({
            url: '/pages/index/newsList',
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                acceptDataFromOpenedPage: function (data) {
                }
            },
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                // res.eventChannel.emit('acceptDataFromOpenerPage', {data: 'test'})
            }
        })
    },
    showNewsDetail(e){
        let item=e.currentTarget.dataset.item;
        wx.navigateTo({
            url: '/pages/index/newsDetail',
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                acceptDataFromOpenedPage: function (data) {
                }
            },
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                 res.eventChannel.emit('acceptDataFromOpenerPage', {newsItem: item})
            }
        })
    }

});
